﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;

using PowerCreator.LMS.Web.Action;
using PowerCreator.LMS.Action.Entity;
public partial class sysadmin_RoleManager_SetCatalogAction :PowerCreator.LMS.Web.Base.BasePage
{
    PowerCreator.LMS.Web.Catalog.Catalog opCatalog = new PowerCreator.LMS.Web.Catalog.Catalog();
    PowerCreator.LMS.Web.Action.Action opAction = new PowerCreator.LMS.Web.Action.Action();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string url = Request.Url.ToString();
            if (Request.QueryString["RoleID"] != null)
            {
                string roleID = Request.QueryString["RoleID"].ToString();
                ViewState["RoleID"] = roleID;
            }
            BinderTree();
        }
    }
    //绑定树控件
    private void BinderTree()
    {
        string search = "";
        string orderList = "";
        int pageSize = opCatalog.QueryCatalogCount(search);
        DataTable dt = opCatalog.QueryCatalog(search, orderList, pageSize, 1);
        ArrayList listt = new ArrayList();
        
        InitTree(null, dt, "0",ref listt);
        int v = listt.Count;

        rptCatalog.DataSource = listt;
        rptCatalog.DataBind();

        search = string.Format(PowerCreator.LMS.Web.Action.Action.FKRoleactionRoleID, ViewState["RoleID"]);
        pageSize = opAction.QueryRoleActionCount(search);
        DataTable dtRoleAction = opAction.QueryRoleAction(search, pageSize, 1);
        Hashtable hs = new Hashtable();
        ArrayList list = new ArrayList();

        for (int i = 0; i < dtRoleAction.Rows.Count; i++)
        {
            hs.Add(dtRoleAction.Rows[i]["FKRoleactionActionID"].ToString(), "1");
        }

        for (int i = 0; i < rptCatalog.Items.Count; i++)
        {
            Label lblCatalogID = (Label)rptCatalog.Items[i].FindControl("lblCatalogID");//chklstAction
            CheckBoxList chk = (CheckBoxList)rptCatalog.Items[i].FindControl("chklstAction");
            if (!string.IsNullOrEmpty(lblCatalogID.Text))
            {
                DataTable dt1 = opAction.QueryCatalogActionByCatalogID(int.Parse(lblCatalogID.Text));
                chk.RepeatColumns = dt1.Rows.Count;
                chk.DataSource = dt1;
                chk.DataTextField = "ActionName";
                chk.DataValueField = "ActionID";
                chk.DataBind();

                for (int j = 0; j < chk.Items.Count; j++)
                {
                    if (hs.ContainsKey(chk.Items[j].Value))
                        chk.Items[j].Selected = true;
                }
            }
        }
    }

    private void InitTree(TreeNode pNode, DataTable dt, string parentid,ref ArrayList listt)
    {
        if (dt.Rows.Count > 0)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[CatalogFatherId]=" + parentid;
            foreach (DataRowView drv in dv)
            {
                TreeNode tempNode = new TreeNode();
                if (pNode == null)
                {
                    //填充根节点
                    tempNode.Text = drv["CatalogName"].ToString() + "&nbsp;" + BuilderHtmlCatalogAction(int.Parse(drv["CatalogID"].ToString()));
                    tempNode.ShowCheckBox = true;
                    tempNode.Value = drv["CatalogID"].ToString();
                    
                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["CatalogName"].ToString();
                    li.Value = drv["CatalogID"].ToString();
                    
                    listt.Add(li);

                    InitTree(tempNode, dt, drv["CatalogID"].ToString(), ref listt);
                }
                else
                {
                    tempNode.Text = drv["CatalogName"].ToString() + BuilderHtmlCatalogAction(int.Parse(drv["CatalogID"].ToString()));
                    tempNode.Value = drv["CatalogID"].ToString();
                    pNode.ChildNodes.Add(tempNode);
                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["CatalogName"].ToString();
                    li.Value = drv["CatalogID"].ToString();

                    listt.Add(li);

                    InitTree(tempNode, dt, drv["CatalogID"].ToString(), ref listt);
                }
            }
        }
    }

    /// <summary>
    /// 创建动态的动作列表复选框
    /// </summary>
    /// <param name="catalogID"></param>
    /// <returns></returns>
    public string BuilderHtmlCatalogAction(int catalogID)
    {
        string html = "<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" >{2}</input>";
        StringBuilder sbHtml = new StringBuilder();
        DataTable dt = opAction.QueryCatalogActionByCatalogID(catalogID);
        sbHtml.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (IsLanageAction)
            {
                string an = GetLanageValueByKey(dt.Rows[i]["ActionVariable"].ToString(), PowerCreator.LMS.Language.LanagetItemType.Action);
                if (!string.IsNullOrEmpty(an))
                    dt.Rows[i]["ActionName"] = an;
            }

            sbHtml.Append(string.Format(html, dt.Rows[i]["ActionAction"].ToString(), dt.Rows[i]["ActionID"].ToString(), dt.Rows[i]["ActionName"]));
            sbHtml.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        return sbHtml.ToString();
    }
  
    protected void btnShowActionID_Click(object sender, EventArgs e)
    {
        string list = "";
        for (int i = 0; i < rptCatalog.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptCatalog.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                if (chkList.Items[j].Selected)
                    list += chkList.Items[j].Value + "'";
            }
        }

        Response.Write(list);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        //全选
        for (int i = 0; i < rptCatalog.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptCatalog.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                chkList.Items[j].Selected = true;
            }
        }
    }
    protected void chkNoAll_CheckedChanged(object sender, EventArgs e)
    {
        //取消
        for (int i = 0; i < rptCatalog.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptCatalog.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                chkList.Items[j].Selected = false;
            }
        }
    }
    protected void chkYes_CheckedChanged(object sender, EventArgs e)
    {
        //反选
        for (int i = 0; i < rptCatalog.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptCatalog.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                if (chkList.Items[j].Selected)
                    chkList.Items[j].Selected = false;
                else
                    chkList.Items[j].Selected = true;
            }
        }
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rptCatalog.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptCatalog.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                string actionid = chkList.Items[j].Value;
                string search = string.Format("FKRoleactionRoleId={0} and FKRoleactionActionID={1}", ViewState["RoleID"], actionid);
                DataTable dt = opAction.QueryRoleAction(search, 1, 1);

                if (chkList.Items[j].Selected)
                {
                    if (dt.Rows.Count == 0)
                    {
                        VOTRoleAction vo = new VOTRoleAction();
                        vo.FKRoleactionActionID = int.Parse(actionid);
                        vo.FKRoleactionCreatorId = GetLoginEntity.UserID;
                        vo.FKRoleactionRoleId = int.Parse(ViewState["RoleID"].ToString());
                        vo.RoleactionCreateTime = DateTime.Now;
                        opAction.AddRoleAction(vo);
                    }
                }
                else
                {
                    if (dt.Rows.Count == 1)
                        opAction.DelRoleAction(int.Parse(dt.Rows[0]["Roleactionid"].ToString()));
                }
            }
        }
    }
}
